<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改版本管理')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-version-edit" th:object="${whVersion}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">版本号：</label>
                <div class="col-sm-8">
                    <input name="versionNo" th:field="*{versionNo}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">apk链接：</label>
                <div class="col-sm-8">
                    <input style="width: 80px;float: left;" id="filePath" name="filePath" class="form-control" type="file"><input style="width: 90%;float: left;" class="form-control" name="versionApk" th:field="*{versionApk}">
                    <!--  <input type="hidden" name="versionApk" th:field="*{versionApk}">
                <div class="file-loading">
                        <input class="form-control file-upload" id="versionApk" name="file" type="file">
                    </div>-->
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">升级说明：</label>
                <div class="col-sm-8">
                    <textarea name="versionDesc" th:field="*{versionDesc}" class="form-control"></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-8">
                    <button style="float: right;" type="button" class="btn btn-primary" onclick="submitHandler();">提交</button>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "whzj/version";
        $("#form-version-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                uploadFile();
            }
        }

        function uploadFile() {
            var formData = new FormData();
            if ($('#filePath')[0].files[0] == null) {
                /*$.modal.alertWarning("请先选择文件路径");
                return false;*/
                formData.append('file', null);
            }else{
                formData.append('file', $('#filePath')[0].files[0]);
            }
            formData.append('id',$("#id").val());
            formData.append('versionNo',$("#versionNo").val());
            formData.append('versionDesc',$("#versionDesc").val());
            $.ajax({
                url: prefix + "/edit",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                success: function(result) {
                    $.operate.successCallback(result);
                }
            });
        }

        $(".file-upload").each(function (i) {
            var val = $("input[name='" + this.id + "']").val()
            $(this).fileinput({
                'uploadUrl': '/common/upload',
                initialPreviewAsData: true,
                initialPreview: [val],
                maxFileCount: 1,
                autoReplace: true
            }).on('fileuploaded', function (event, data, previewId, index) {
                debugger;
                $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            })
            $(this).fileinput('_initFileActions');
        });

        function saveSetting() {
            var formData = new FormData();
            formData.append('id',$("#id").val());
            formData.append('versionNo',$("#versionNo").val());
            formData.append('versionApk',$("#versionApkUrl").val());
            formData.append('versionDesc',$("#versionDesc").val());
            $.ajax({
                url: prefix + "/edit",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                success: function(result) {
                    $.operate.successCallback(result);
                }
            });
        }
    </script>
</body>
</html>